package logic

import (
	"2day1zy/model/mysql"
	"2day1zy/model/redisinit"
	"2day1zy/userrpc/internal/svc"
	"2day1zy/userrpc/userrpc"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type UeerUpLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUeerUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UeerUpLogic {
	return &UeerUpLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UeerUpLogic) UeerUp(in *userrpc.UeerUpRequest) (*userrpc.UeerUpResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Where("tel = ?", in.Tel).Find(&user)

	get := redisinit.Rdb.Get("sendsms" + in.Tel)
	if get.Val() != in.Smscode {
		return nil, fmt.Errorf("短信验证码错误")
	}

	if user.ID == 0 {
		user = mysql.User{
			Tel: in.Tel,
		}
		mysql.DB.Debug().Create(&user)
	}

	return &userrpc.UeerUpResponse{
		Id: int64(user.ID),
	}, nil
}
